package com.zxb.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

/**
 * 基于redis+lua限流配置
 *
 * spring cloud alibaba官方提供了RequestRateLimiter过滤器工厂，
 * 基于redis+lua脚本方式采用令牌桶算法实现了限流。
 * https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-requestratelimiter-gatewayfilter-factory
 *
 * @author zhuxiongbin
 * @date 2023/03/26
 */
@Configuration
public class RateLimiterConfig {

    @Bean
    public KeyResolver keyResolver(){
        //url限流  http://localhost:8888/user/findOrderByUserId/1
        //return exchange -> Mono.just(exchange.getRequest().getURI().getPath());

        //参数限流 http://localhost:8888/user/findOrderByUserId/1?user=zxb
        return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
    }

}
